package cool.taomu.toolkit.service.kademlia.utils

import cool.taomu.toolkit.utils.StringUtils
import java.security.SecureRandom

class KademliaUtils {

	def static createNodeId() {
		var random = new SecureRandom();
		var bytes = newByteArrayOfSize(20);
		random.nextBytes(bytes);
		val binaryStringBuilder = new StringBuilder();
		bytes.forEach [
			var uint = it.bitwiseAnd(0xFF);
			var str = String.format("%8s", Integer.toBinaryString(uint)).replace(' ', '0');
			binaryStringBuilder.append(str);
		]
		return stringTo160Bit(binaryStringBuilder.toString());
	}

	
	def static stringTo160Bit(String string) {
		return StringUtils.stringTo160Bit(string)
	}
}
